package com.cskaoyan.controller;

import com.cskaoyan.bean.data.CommentPostData;
import com.cskaoyan.bean.data.PageData;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.vo.CommentPostVo;
import com.cskaoyan.service.CommentService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("wx/comment")
public class WxCommentController {

    @Autowired
    CommentService commentService;

    //查看评论列表
    @GetMapping("list")
    public BaseRespVo list(Integer valueId, Byte type, Integer page, Integer limit,Boolean showType)
    {
        PageData data = commentService.list(valueId,type,page,limit,showType);

        return BaseRespVo.ok(data);
    }

    //查看评论条数
    @GetMapping("count")
    public BaseRespVo count(Integer valueId, Byte type)
    {
        Map<String,Integer> data = commentService.count(valueId,type);

        return BaseRespVo.ok(data);
    }


    //专题精选的评论发表

    @PostMapping("post")
    public BaseRespVo post(@RequestBody CommentPostVo commentPostVo)
    {
        Subject subject = SecurityUtils.getSubject();
        //如果登录
        if(subject.isAuthenticated()){
            Integer userId = (Integer) subject.getPrincipal();
            CommentPostData data = commentService.post(commentPostVo,userId);
            return BaseRespVo.ok(data);
        }else{
            return BaseRespVo.unAuthc();
        }
    }
}
